package yanyan.com.tochar.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import yanyan.com.tochar.FunsBlackAndWhiteImgActivity;
import yanyan.com.tochar.FunsBuyCardAtivity;
import yanyan.com.tochar.FunsDbActivity;
import yanyan.com.tochar.FunsDomainNameSelectActivity;
import yanyan.com.tochar.FunsGetSourceCodeAtivity;
import yanyan.com.tochar.FunsHiddenImgActivity;
import yanyan.com.tochar.FunsImgToTextActivity;
import yanyan.com.tochar.FunsIpSelectActivity;
import yanyan.com.tochar.FunsMusicActivity;
import yanyan.com.tochar.FunsOrzPoetryActivity;
import yanyan.com.tochar.FunsPixelImgActivity;
import yanyan.com.tochar.FunsRepeatGameNmActivity;
import yanyan.com.tochar.FunsShotLinkActivity;
import yanyan.com.tochar.FunsSketchActivity;
import yanyan.com.tochar.FunsSpecialFontActivity;
import yanyan.com.tochar.FunsVideoResolveActivity;
import yanyan.com.tochar.FunsWebAppActivity;
import yanyan.com.tochar.QqCardActivity;
import yanyan.com.tochar.R;
import yanyan.com.tochar.adpter.CommonGridViewAdapter;
import yanyan.com.tochar.beans.CommonGridViewBean;
import yanyan.com.tochar.beans.WebAppInfo;
import yanyan.com.tochar.funsactivity.FunsGetAndPutVoiceActivity;
import yanyan.com.tochar.funsactivity.FunsImageToUrlActivity;
import yanyan.com.tochar.funsactivity.FunsNoticeActivity;
import yanyan.com.tochar.funsactivity.FunsQqLongCharActivity;
import yanyan.com.tochar.funsactivity.FunsQzchatActivity;
import yanyan.com.tochar.funsactivity.FunsSplitImageActivity;
import yanyan.com.tochar.funsactivity.FunsVoiceUpendActivity;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.EmptyUtils;
import yanyan.com.tochar.utils.ToastUtil;

public class Funs extends Fragment {
   private List<CommonGridViewBean> gidViewBeanList;
    private View view;
    private GridView gridView;
    private Context context;
    private CommonGridViewAdapter commonGridViewAdapter;
    int[] imgs={R.mipmap.music,R.mipmap.video,R.mipmap.baike,R.mipmap.xiaosou,R.mipmap.funs_card,R.mipmap.funs_game,R.mipmap.funs_db};
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.main_gn,container,false);
        gridView=view.findViewById(R.id.gn_grid);
        //初始化gridview
        initGridView();
        return view;
    }
    /**
     * 初始化gridview
     * TODO:超过屏幕能看到的数量,上下滑动会错乱
     */
    private void initGridView() {
        gidViewBeanList=new ArrayList<>();
        CommonGridViewBean b=new CommonGridViewBean("火车票生成",R.mipmap.funs_card,new FunsBuyCardAtivity(),null);
        gidViewBeanList.add(b);
        CommonGridViewBean b2=new CommonGridViewBean("王者重复名",R.mipmap.funs_game,new FunsRepeatGameNmActivity(),null);
        gidViewBeanList.add(b2);
        CommonGridViewBean b3=new CommonGridViewBean("噪音检测",R.mipmap.funs_db,new FunsDbActivity(),null);
        gidViewBeanList.add(b3);
        CommonGridViewBean b4=new CommonGridViewBean("藏头诗生成",R.mipmap.funs_poe,new FunsOrzPoetryActivity(),null);
        gidViewBeanList.add(b4);
        CommonGridViewBean b5=new CommonGridViewBean("特殊字体",R.mipmap.funs_font,new FunsSpecialFontActivity(),null);
        gidViewBeanList.add(b5);
        CommonGridViewBean b6=new CommonGridViewBean("IP地址查询",R.mipmap.funs_ip,new FunsIpSelectActivity(),null);
        gidViewBeanList.add(b6);
        CommonGridViewBean b7=new CommonGridViewBean("域名备案查询",R.mipmap.funs_domain,new FunsDomainNameSelectActivity(),null);
        gidViewBeanList.add(b7);
        CommonGridViewBean b8=new CommonGridViewBean("网页源码获取",R.mipmap.funs_source,new FunsGetSourceCodeAtivity(),null);
        gidViewBeanList.add(b8);
        CommonGridViewBean b9=new CommonGridViewBean("短连接生成",R.mipmap.funs_link,new FunsShotLinkActivity(),null);
        gidViewBeanList.add(b9);

        CommonGridViewBean b10=new CommonGridViewBean("隐藏图制作",R.mipmap.funs_hidden,new FunsHiddenImgActivity(),null);
        gidViewBeanList.add(b10);

        CommonGridViewBean b11=new CommonGridViewBean("黑白照片生成",R.mipmap.funs_bw,new FunsBlackAndWhiteImgActivity(),null);
        gidViewBeanList.add(b11);

        CommonGridViewBean b12=new CommonGridViewBean("像素图",R.mipmap.funs_pixel,new FunsPixelImgActivity(),null);
        gidViewBeanList.add(b12);

        CommonGridViewBean b13=new CommonGridViewBean("图片文字化",R.mipmap.funs_text_img,new FunsImgToTextActivity(),null);
        gidViewBeanList.add(b13);

        CommonGridViewBean b14=new CommonGridViewBean("素描图片制作",R.mipmap.funs_sketch,new FunsSketchActivity(),null);
        gidViewBeanList.add(b14);

        CommonGridViewBean b15=new CommonGridViewBean("图片分割",R.mipmap.funs_split,new FunsSplitImageActivity(),null);
        gidViewBeanList.add(b15);

        CommonGridViewBean b16=new CommonGridViewBean("QQ强制会话",R.mipmap.funs_qq_chat,new FunsQzchatActivity(),null);
        gidViewBeanList.add(b16);

        CommonGridViewBean b17=new CommonGridViewBean("长文字气泡",R.mipmap.funs_qp,new FunsQqLongCharActivity(),null);
        gidViewBeanList.add(b17);

        CommonGridViewBean b18=new CommonGridViewBean("图片转链接",R.mipmap.funs_img_link,new FunsImageToUrlActivity(),null);
        gidViewBeanList.add(b18);

        CommonGridViewBean b19=new CommonGridViewBean("声音倒放",R.mipmap.funs_voice,new FunsVoiceUpendActivity(),null);
        gidViewBeanList.add(b19);

        CommonGridViewBean b20=new CommonGridViewBean("音频提取及合并",R.mipmap.funs_video_get_put,new FunsGetAndPutVoiceActivity(),null);
        gidViewBeanList.add(b20);




//        CommonGridViewBean b12=new CommonGridViewBean("付费音乐下载",R.mipmap.funs_code,new FunsMusicActivity(),null);
//        gidViewBeanList.add(b12);

        if(CoreUtil.webAppInfos!=null && CoreUtil.webAppInfos.size()>0){
            Random random=new Random();
            for (WebAppInfo webAppInfo:CoreUtil.webAppInfos){
                int index=  random.nextInt(imgs.length-1);
                CommonGridViewBean web=new CommonGridViewBean(webAppInfo.getTitle(),imgs[index],new FunsWebAppActivity(),null);
                web.setWebAppInfo(webAppInfo);
                gidViewBeanList.add(web);
            }
        }else {
            getOtherUrl();
        }

         commonGridViewAdapter=new CommonGridViewAdapter(context,gidViewBeanList);
        gridView.setAdapter(commonGridViewAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CommonGridViewBean gridViewBean=gidViewBeanList.get(position);
                if(gridViewBean.getActivity()!=null){
                    Intent in=new Intent();
                    in.setClass(context,gridViewBean.getActivity().getClass());
                    if(gridViewBean.getWebAppInfo()!=null){
                        in.putExtra("webapp", gridViewBean.getWebAppInfo());
                    }
                    startActivity(in);
                }
            }
        });


    }

    private void getOtherUrl(){
        new Thread(new Runnable() {
            @Override
            public void run() {

                Random random=new Random();
                for(int i=0;i<3;i++){
                    try{
                        if(CoreUtil.webAppInfos!=null && CoreUtil.webAppInfos.size()>0){
                            for (WebAppInfo webAppInfo:CoreUtil.webAppInfos){
                              int index=  random.nextInt(imgs.length-1);
                                CommonGridViewBean b10=new CommonGridViewBean(webAppInfo.getTitle(),imgs[index],new FunsWebAppActivity(),null);
                                b10.setWebAppInfo(webAppInfo);
                                gidViewBeanList.add(b10);
                            }
                            commonGridViewAdapter.notifyDataSetChanged();
                        }else {
                            Thread.sleep(1000*5);
                        }

                    }catch (Exception e){

                    }
                }
            }
        }).start();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=getActivity();
    }
}
